import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TodoScaffold extends StatefulWidget {
  TodoScaffold({
    Key key,
    @required this.pages,
    this.bottomNavigationItems,
    this.mini = true,
    this.backgroundColor,
  }) : super(key: key);

  final Color backgroundColor;
  final bool mini;
  final List<Widget> pages;
  final List<BottomNavigationBarItem> bottomNavigationItems;

  @override
  State<StatefulWidget> createState() => _TodoScaffold();
}

class _TodoScaffold extends State<TodoScaffold>
    with SingleTickerProviderStateMixin {
  bool isCheck = false;
  int selected = 0;
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 400),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  void _onTap(int index) {
    setState(() {
      selected = index;
    });
  }

  Widget _buildBody() {
    return widget.pages[selected];
  }

  Widget _buildBottomNavigationBar() {
    if (isCheck && widget.bottomNavigationItems != null) {
      return BottomNavigationBar(
        onTap: _onTap,
        currentIndex: selected,
        items: widget.bottomNavigationItems,
      );
    }
    return null;
  }

  Widget _buildFloatingActionButton() {
    return FloatingActionButton(
      mini: widget.mini,
      child: AnimatedIcon(
        icon: AnimatedIcons.menu_home,
        progress: _animationController,
      ),
      onPressed: () {
        setState(() {
          isCheck = !isCheck;
          isCheck
              ? _animationController.forward()
              : _animationController.reverse();
        });
        if (!isCheck && selected != 0) _onTap(0);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: widget.backgroundColor,
      body: _buildBody(),
      bottomNavigationBar: _buildBottomNavigationBar(),
      floatingActionButton: _buildFloatingActionButton(),
    );
  }
}
